{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "31d3ecd0",
   "metadata": {},
   "source": [
    "## 数组操作\n",
    "\n",
    "### 数值相关的操作\n",
    "\n",
    "可以使用`.sum()`方法进行求和："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5028ed9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6de37650",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[1, 2, 3], [4, 5, 6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f40ac54b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "426a82c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea8c4a75",
   "metadata": {},
   "source": [
    "默认对所有元素求和，可以通过`axis`参数指定求和维度："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1a33c8a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 7, 9])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "32d8fbc7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6, 15])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum(axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46226450",
   "metadata": {},
   "source": [
    "指定维度时，相当于沿着指定的维度进行求和，因此，对于形状为(2,3)的数组，指定第0维会将第0维消去，得到形状为(3,)的数组，指定第1维会将第1维消去，得到形状为(2,)的数组。\n",
    "\n",
    "与之对应还有个`np.sum()`函数，得到相同的操作结果："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1f81abf2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df25459f",
   "metadata": {},
   "source": [
    "该函数可以作用在与数组类似的对象上："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6483a601",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum([[1, 2, 3], [4, 5, 6]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce12f9c3",
   "metadata": {},
   "source": [
    "`.prod()`方法和`np.prod()`函数可以实现求积操作，与`.sum()`方法类似："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e65e352c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "720"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.prod()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "882fe4d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  6, 120])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.prod(a, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68454429",
   "metadata": {},
   "source": [
    "`.max()`和`.min()`函数可以实现求最值的操作，也支持指定维度："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0b1b0ab8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "80277d1e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8aaf8be6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 6])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.max(axis=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fa70eaac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.min(axis=0)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "4178efcc",
   "metadata": {},
   "source": [
    "对应的函数形式分别为`np.max()`和`np.min()`。\n",
    "\n",
    "`.argmax()`和`.argmin()`函数可以分别返回最大值和最小值的位置："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "60860b02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.argmax()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa89c535",
   "metadata": {},
   "source": [
    "a是一个(2,3)的数组，索引5是不存在的，`.argmax()`方法返回的是将a看成一维数组时最大值的位置。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "04b15a50",
   "metadata": {},
   "source": [
    "指定维度时，它返回的是对应维度中最值的位置："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "eb05485c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.argmin(axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10fd80d4",
   "metadata": {},
   "source": [
    "对应的函数形式分别为`np.argmax()`和`np.argmin()`。\n",
    "\n",
    "均值`.mean()`方法和`np.mean()`函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b85046e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.5"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "329a5e7e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2., 5.])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.mean(axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6e6d48c",
   "metadata": {},
   "source": [
    "标准差`.std()`方法和`np.std()`函数，方差`.var()`方法和`np.var()`函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f6ede216",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.707825127659933"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e3ce30f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.81649658, 0.81649658])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.std(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "37703dfe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.9166666666666665"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.var()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "0c96ffbc",
   "metadata": {},
   "source": [
    "`.round()`方法会将数组近似到整数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "71415f45",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([1.35, 2.5, 1.5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "17b4d9c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 2.])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.round()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a6f60fd",
   "metadata": {},
   "source": [
    "### 形状相关的操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f35f5cc",
   "metadata": {},
   "source": [
    "使用`.shape`属性可以直接修改数组的形状："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "6bceffa4",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([8, 6, 5, 7, 1, 4, 2, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f81099f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8,)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "ce8815aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 6, 5, 7, 1, 4, 2, 3])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b5a5d96f",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.shape = 2, 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c5d1ab75",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 6, 5, 7],\n",
       "       [1, 4, 2, 3]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84f5a45f",
   "metadata": {},
   "source": [
    "可以使用`.reshape()`方法得到一个新数组，但是不改变原来的数组："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ca3b263b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 6],\n",
       "       [5, 7],\n",
       "       [1, 4],\n",
       "       [2, 3]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(4, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "3dbf40a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 6, 5, 7],\n",
       "       [1, 4, 2, 3]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5499d2c4",
   "metadata": {},
   "source": [
    "形状可以增加一个`-1`参数，当某个维度为`-1`时，`NumPy`会自动根据其他维度计算："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "5cc4530a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 6],\n",
       "       [5, 7],\n",
       "       [1, 4],\n",
       "       [2, 3]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(-1, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4477605f",
   "metadata": {},
   "source": [
    "可以调用`.resize()`方法改变原来数组的形状："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "8f707b2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.resize(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "1a56d62c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 6, 5, 7, 1, 4, 2, 3])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73f1931c",
   "metadata": {},
   "source": [
    "`.resize()`方法不支持`-1`参数，但是支持形状不对应的修改，缺少的部分会补`0`："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "656cb5a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.array([1, 2, 3, 4, 5, 6, 7, 8])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "1f4e84cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "b.resize(2, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "6767a811",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4, 5],\n",
       "       [6, 7, 8, 0, 0]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "45767dcb",
   "metadata": {},
   "source": [
    "np.newaxis可以扩展数组的维度，通常配合索引使用，增加一个大小为1的新维度："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "1a9c98ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "6460cc7e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3,)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "556789b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[np.newaxis, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "2295a437",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3,)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "745bdd49",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0],\n",
       "        [1],\n",
       "        [2]]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[np.newaxis, :, np.newaxis]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "82a4a6be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3,)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e0d5fda7",
   "metadata": {},
   "source": [
    "`np.newaxis`是`None`的别名，也可以直接使用`None`代替它："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "460d0fcc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.newaxis is None"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f03a582",
   "metadata": {},
   "source": [
    "`.squeeze()`方法返回一个去掉所有大小为1的维度的新数组，原数组不改变："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "716e9fda",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.shape = 1, 3, 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "82b40e16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0],\n",
       "        [1],\n",
       "        [2]]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "44b0ef87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.squeeze()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "cc00ed23",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 3, 1)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37cffdc4",
   "metadata": {},
   "source": [
    "数组的转置："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "c13cccfc",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "ab31a0e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.shape = 5, 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "c9c304db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10, 11],\n",
       "       [12, 13, 14, 15, 16, 17],\n",
       "       [18, 19, 20, 21, 22, 23],\n",
       "       [24, 25, 26, 27, 28, 29]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "be7da653",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  6, 12, 18, 24],\n",
       "       [ 1,  7, 13, 19, 25],\n",
       "       [ 2,  8, 14, 20, 26],\n",
       "       [ 3,  9, 15, 21, 27],\n",
       "       [ 4, 10, 16, 22, 28],\n",
       "       [ 5, 11, 17, 23, 29]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "7e1afde2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  6, 12, 18, 24],\n",
       "       [ 1,  7, 13, 19, 25],\n",
       "       [ 2,  8, 14, 20, 26],\n",
       "       [ 3,  9, 15, 21, 27],\n",
       "       [ 4, 10, 16, 22, 28],\n",
       "       [ 5, 11, 17, 23, 29]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.transpose()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "51290169",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.shape = 1, 2, 3, 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "9edc1b67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 3, 2, 1)"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.T.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ecbe22fe",
   "metadata": {},
   "source": [
    "### 数组的连接操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "9f42dc67",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "d58193e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.shape = 2, 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "6762aeb6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "9852a426",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.arange(10, 16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "afe2d292",
   "metadata": {},
   "outputs": [],
   "source": [
    "b.shape = 2, 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "3dabb05b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 11, 12],\n",
       "       [13, 14, 15]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "87b10bce",
   "metadata": {},
   "source": [
    "连接两个数组可以使用`np.concatenate()`函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "1e3aa141",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.concatenate((a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "573b2d08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 3)"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "52d4570b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2],\n",
       "       [ 3,  4,  5],\n",
       "       [10, 11, 12],\n",
       "       [13, 14, 15]])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5b21769",
   "metadata": {},
   "source": [
    "默认情况下，`np.concatenate()`函数会沿着第0维开始进行连接。连接的维度可以通过`axis`指定："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "84118815",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.concatenate((a, b), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "337d3b79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 6)"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e75d826",
   "metadata": {},
   "source": [
    "理论上说，a和b的形状一样，还可以让他们沿着第2维进行连接，得到一个(2,3,2)的数组。但是`np.concatenate()`函数不支持直接使用这个的操作。为了解决这个问题，可以使用`np.atleast_1d(),np.atleast_2d(),np.atleast_3d()`函数将数组扩展："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "a6079884",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.concatenate(np.atleast_3d(a, b), axis=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "1fef4729",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3, 2)"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.shape"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "5058dd28",
   "metadata": {},
   "source": [
    "`np.atleast_xd()`函数系列的运作规则如下：\n",
    "\n",
    "- `np.atleast_xd()`函数系列对于大于等于x维的数组不起作用。\n",
    "- 对于形状为(N,)的数组，`np.atleast_2d()`函数将其转变为形状为(1,N)的数组。\n",
    "- 对于形状为(N,)的数组，`np.atleast_3d()`函数将其转变为形状为(1,N,1)的数组。\n",
    "- 对于形状为(M,N)的数组，`np.atleast_3d()`函数将其转变为形状为(M,N,1)的数组。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "fb0809b0",
   "metadata": {},
   "source": [
    "对于常用的沿着第0,1,2三维的操作，NumPy提供了以下三个函数`np.vstack(),np.hstack(),np.dstack()`，完成各个方法的连接操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "36389191",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "b00fe947",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "655c4fec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2],\n",
       "       [ 3,  4,  5],\n",
       "       [10, 11, 12],\n",
       "       [13, 14, 15]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "f47cfbc4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2, 10, 11, 12],\n",
       "       [ 3,  4,  5, 13, 14, 15]])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "95432d4e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0, 10],\n",
       "        [ 1, 11],\n",
       "        [ 2, 12]],\n",
       "\n",
       "       [[ 3, 13],\n",
       "        [ 4, 14],\n",
       "        [ 5, 15]]])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dstack((a, b))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f69a7db2",
   "metadata": {},
   "source": [
    "### 数组的四则运算，点乘和矩阵运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "6b749367",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[1, 2], [3, 4]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "912233c3",
   "metadata": {},
   "source": [
    "四则运算："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "9113923d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  4],\n",
       "       [ 9, 16]])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "521d395b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 4],\n",
       "       [6, 8]])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "a00b4c6d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1.],\n",
       "       [1., 1.]])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a / a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "f937098d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0],\n",
       "       [0, 0]])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a - a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "b7db3470",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  4],\n",
       "       [ 9, 16]])"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "1a480d10",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + 2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "662c708a",
   "metadata": {},
   "source": [
    "点乘，即矩阵运算："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "83dddd4c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7, 10],\n",
       "       [15, 22]])"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dot(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "6b2486f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7, 10],\n",
       "       [15, 22]])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(a, a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b63255e0",
   "metadata": {},
   "source": [
    "Python 3提供了`@`操作符完成点乘操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "4eab632d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7, 10],\n",
       "       [15, 22]])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a @ a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bef828ae",
   "metadata": {},
   "source": [
    "### 数组的数学操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "9f5a1bdb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.141592653589793"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.pi"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d837b324",
   "metadata": {},
   "source": [
    "三角函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "8c54c28e",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.linspace(0, np.pi, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "ab15247c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.        , 1.04719755, 2.0943951 , 3.14159265])"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "ce4785d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1. ,  0.5, -0.5, -1. ])"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cos(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "70a4d07b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.        ,  2.84965391,  8.1205274 , 23.14069263])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.exp(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "41532c28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.        , 1.02332671, 1.44720251, 1.77245385])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac05738a",
   "metadata": {},
   "source": [
    "### 数组的逻辑与比较操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "b82d8bab",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "f6997b53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False,  True,  True])"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a > 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "d79f037b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 5])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[a > 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "f4feb22d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True,  True,  True,  True])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a <= a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf16e704",
   "metadata": {},
   "source": [
    "不能用 `==` 符号判断两个数组是否相等："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "3f3ca150",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True,  True,  True,  True])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a == a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "972350fa",
   "metadata": {},
   "source": [
    "判断相等需要用 `np.all()` 方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "8e0b93f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.all(a == a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d82504de",
   "metadata": {},
   "source": [
    "不过考虑到浮点数的精度问题，所以一般用 `np.allclose()` 方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "34424397",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.allclose(a, a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4f7b7d4",
   "metadata": {},
   "source": [
    "`np.nan`是一个特殊的值，它做判断的时候当`False`用，且与任何数进行比较都是`False`，包括它自己："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "162db9bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.nan == np.nan"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "160b6348",
   "metadata": {},
   "source": [
    "为了判断一个值是否为`np.nan`，需要使用`np.isnan()`函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "b26e74d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.isnan(np.nan)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "13d58379",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.array([np.inf, -np.inf, np.nan])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "a6da0cef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False,  True])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.isnan(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab3bc4ce",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
